﻿using CoreShop.App.Units;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using CoreShop.Services.Services.Report;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using static CoreShop.Library.GlobalEnumVars;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace CoreShop.App
{
    public partial class frmBalance : UIForm
    {
        public bool returnValue;

        List<ReportBalance> lstSource = new List<ReportBalance>();
        public delegate void CloseFormPage(int pageIndex);
        public int mPageIndex = 0;
        public string number = "";

        long foodTypeId = 0;
        string foodTypeName = "";
        public frmBalance(int _mPageIndex)
        {
            mPageIndex = _mPageIndex;
            InitializeComponent();
        }
        public frmBalance(string _number)
        {
            number = _number;
            InitializeComponent();
        }
        public frmBalance()
        {
            InitializeComponent();
        }

        #region 加载信息列表
        private void LoadDataList()
        {
            DateTime startDate = CommFunc.ConvertToDateTime(dateTimeRange.DateTimeStar);
            DateTime endDate = CommFunc.ConvertToDateTime(dateTimeRange.DateTimeEnd);
            int count = 0;
            ReportBalance sumRow = new ReportBalance();
            lstSource = new corereportServices().Getcorecmsuserbalance(ref count, mPageIndex, btnPg.PageSize, startDate, endDate, this.txtNumber.Text, ref sumRow);
            btnPg.TotalCount = count;
            this.binDetail.DataSource = lstSource;
            this.dgvPayList.AutoGenerateColumns = false;
            //this.dgvPayList.DataSource = lstSource;
            //uiDataGridViewFooter1.DataGridView = sumRow;
            //设置统计绑定的表格
            uiDataGridViewFooter1.DataGridView = dgvPayList;
            uiDataGridViewFooter1["createTime"] = "合计：";
            uiDataGridViewFooter1["money"] = CommFunc.ConvertToString(sumRow.money);
            uiDataGridViewFooter1["giftAmount"] = CommFunc.ConvertToString(sumRow.giftAmount);
        }

        #endregion
        private void Food_Load(object sender, System.EventArgs e)
        {
            mPageIndex = 1;
            dateTimeRange.DateTimeStar = CommFunc.ConvertToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00").ToString("yyyy-MM-dd HH:mm:ss");
            dateTimeRange.DateTimeEnd = CommFunc.ConvertToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59").ToString("yyyy-MM-dd HH:mm:ss");
            LoadDataList();
        }
        private void btnSerach_Click(object sender, EventArgs e)
        {
            mPageIndex = 1;
            LoadDataList();
        }

        private void dgvFoodList_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if (binDetail.Current != null)
            {
                ReportBalance reportBalance = (ReportBalance)this.binDetail.Current;
                var frm = new frmBalanceInfo(reportBalance);
                frm.ShowDialog();
                frm.Dispose();
                LoadDataList();
            }
        }

        private void btnPg_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            mPageIndex = pageIndex;
            LoadDataList();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            //btnExport_Click
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Export Excel File To";
            saveFileDialog.ShowDialog();
            string fileName = saveFileDialog.FileName;
            if (fileName == "")
            {
                return;
            }
            Dictionary<string, DataGridView> gridViews = new Dictionary<string, DataGridView>();
            gridViews.Add("班次营业统计", dgvPayList);
            if (ExportUtil.NPOIExportForDataGridview(gridViews, fileName))
            {
                MessageBox.Show("导出Excel成功！");
            }
        }

        private void btnBalanceInfo_Click(object sender, EventArgs e)
        {
            if (binDetail.Current != null)
            {
                ReportBalance reportBalance = (ReportBalance)this.binDetail.Current;
                var frm = new frmBalanceInfo(reportBalance);
                frm.ShowDialog();
                frm.Dispose();
                LoadDataList();
            }
            else {
                UIMessageBox.Show("请选择一行数据进行查看", "系统提示", UIStyle.Red, UIMessageBoxButtons.OK);
            }
        }
    }
}